package org.suyafei.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author 宿某人
 * @Date: 2021/12/12/9:42 下午
 * @Description:
 */
@Configuration
@Slf4j
public class SnowFlakeWordIdConfig {

  static {

      /**
       * 动态指定sharding jdbc 的雪花算法中的属性work.id属性
       * 通过调用System.setProperty()的方式实现,可用容器的 id 或者机器标识位
       * workId最大值 1L << 100，就是1024，即 0<= workId < 1024
       * {@link SnowflakeShardingKeyGenerator#getWorkerId()}
       *
       */
      try {
          InetAddress inetAddress = Inet4Address.getLocalHost();
          String hostAddress = inetAddress.getHostAddress();
          String workId = Math.abs(hostAddress.hashCode()) % 1024 + "";
          System.setProperty("workId", workId);
          log.info("当前服务的workId-------{}", workId);
      } catch (Exception e) {
          log.error("获取workId失败"+e);
      }
  }
}
